#!/usr/bin/perl -w
use strict;
use English;
use Getopt::Long;
use File::stat;
use POSIX qw(ceil floor);

my $DISK_SN_SAVE_FILE = "/var/OPM/scripts/DiskSN.txt";
my ($dir, $usage, $base, $checksum);

my (%SNINFO,%SNPATHS,%SNSHORTPATHS,%SNSLICES,%SNSLICEUSAGE,%PATHSSN,%SNVERITAS);

#GetOptions("dir=s"=>\$dir, "sum" => \$checksum)||die "$usage";
#$usage = "$PROGRAM_NAME ";
#unless( defined($dir)) {die "$usage";}

my $hostname = `/bin/uname -n`; chomp $hostname;
my $hostid = `/usr/sbin/hostid`; chomp $hostid;
my $dt = `/bin/date +"%Y-%m-%d %H:%M:%S"`; chomp $dt;

my $outfile = "/var/OPM/data/${hostname}_disks.out";
open (OUT, ">$outfile");
print OUT '<uname -n>',"\n",$hostname,"\n",'</uname -n>', "\n";
print OUT '<hostid>',"\n",$hostid,"\n",'</hostid>', "\n";
print OUT '<date>',"\n",$dt,"\n",'</date>', "\n";

GetDiskInf();
PrintFSInfo();
GetVxDg();
close OUT;

sub PrintFSInfo{
	print OUT "<fs>\n";
	{
	my @TMP = `/bin/df -kP |/bin/egrep -v "^fd |^/proc |^mnttab |^tmpfs|Filesystem|^Filesystem \*kbytes"`;
	foreach my $tmp (@TMP){
		chomp $tmp;
		$tmp =~ s/^\s+//; $tmp =~ s/\s+$//; $tmp =~ s/\s+/\t/g;
		my($fs,$size,$used,$free,$cap,$mp) = split /\t/,$tmp;
		$size = $size / 1024 / 1024;
		$used = $used / 1024 / 1024;
		$free = $free / 1024 / 1024;	
		print OUT "$fs\t$size\t$used\t$free\t$cap\t$mp\n";
	}
	}
	print OUT "</fs>\n";
}

sub GetVxDg{
        print OUT "<dg>\n";
                my $dgfree;
                my $dgstate;
                my $dgid;
                my @DGLIST = `/usr/sbin/lsvg -o`;
                foreach my $dgname (@DGLIST){
                        chomp $dgname;
                        open(DATA, "/usr/sbin/lsvg $dgname|") || die "erreur";
                        while(<DATA>){
                                if($_ =~ /^.*FREE PPs.*\(([0-9]*)/){
                                        $dgfree=$1; $dgfree = $dgfree / 1024;
                                }
                                if($_ =~ /^VG STATE:\s+(\S+)\s/){
                                        $dgstate=$1;
                                }
                                if($_ =~ /^.*VG IDENTIFIER:\s+(\S+)\s/){
                                        $dgid=$1;
                                }
                        }
                        close (DATA);

                        print OUT "$dgname\t$dgstate\t$dgid\t$dgfree\n";
                }
        print OUT "</dg>\n";
}

sub GetDiskInf {
	print OUT "<disk>\n";

	my @a_lspv = `/usr/sbin/lspv`;
	foreach my $tmp (@a_lspv){
		$tmp =~ s/\s+/ /g;
		my @_tmp = split /\s/, $tmp;
		my $pv = $_tmp[0] || "-";
		my $sn = $_tmp[1] || "-";
		my $dgname = $_tmp[2] || "-";
		my $paths = "/dev/$pv";
		my $disksize = `/usr/sbin/bootinfo -s $pv`; chomp $disksize; $disksize = $disksize / 1024 ;
		my $lsdev = `/usr/sbin/lsdev -l $pv`; $lsdev =~ s/\s+/ /g;
		my $serial = `/usr/bin/odmget -q "name=$pv and attribute =unique_id" CuAt | grep value | cut -d'"' -f2`; chomp $serial;
		#my $vpd = `lscfg -vl $pv`; chomp $vpd; my @a_vpd = split(/\s+/, $vpd); $vpd = $a_vpd[2];
		my $vpd = `lscfg -vl $pv`; chomp $vpd; my @a_vpd = split(/\s+/, $vpd); $vpd = $a_vpd[2]; @a_vpd = split(/-/, $vpd); $vpd = $a_vpd[$#a_vpd];
		my @a_lsdev = split /\s/, $lsdev;
		my $status = $a_lsdev[1] || "-";
		my $disktype = $a_lsdev[2] || "-";
		print OUT "$pv\t$sn\t$dgname\t$disktype\t$status\t$paths\t$disksize\t$vpd\t$serial\n";
	}
	print OUT "</disk>\n";
}
`chown saunix $outfile`;
